/*
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-05-31 17:30:19
 * @LastEditTime: 2022-06-06 13:36:35
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description: 
 */
var greeting = 'My name is ${name}, age ${age}, I am a ${job.jobName}';
var employee = {
  name: 'XiaoMing',
  age: 11,
  job: {
    jobName: 'designer',
    jobLevel: 'senior'
  }
};
// // 方法一
// String.prototype.render = function (obj) {
//   with (obj) {
//     return eval('`' + this + '`')
//   }
// }
// 方法二
String.prototype.render = function (obj) {
  // 利用了ES6的解构、对象keys新方法，在函数内部解构并自动展开变量
  eval(`var {${Object.keys(obj).join(',')}} = obj`)
  // 利用eval使字符串直接作为ES6解析
  return eval('`' + this + '`')
}


var result = greeting.render(employee);
console.log(result);